package com.oss;


import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.util.List;

/**
 * 中间件接口
 */
@Slf4j
@RestController
@RequestMapping("/api")
@AllArgsConstructor
@Api(value = "OSS-API", tags = "OSS-API")
public class MiddlewareApi {

   FileUtilService fileUtilService;

    /**
     * 上传文件
     */
    @PostMapping("/uploads")
    @ApiOperation(value = "上传文件")
    public R uploads(@RequestParam("file") List<MultipartFile> file) {
        List<String> xs = fileUtilService.uploads(file);
        return R.success(xs);
    }

    /**
     * 下载文件
     */
    @GetMapping("/download")
    @ApiOperation(value = "下载文件")
    public void download(@RequestParam String icon, HttpServletResponse response) throws IOException {
        //通知浏览器以附件形式下载
        response.setHeader("Content-Disposition",
                "attachment;filename=" + new String(icon.getBytes(), StandardCharsets.ISO_8859_1));
        this.fileUtilService.download(response.getOutputStream(), icon);
    }


}
